/**
 * 状态码
 * 0 失败
 * 1 成功
 * 2 权限不足
 * ...其它可自行定义
 */
export enum ResultCode {
  Fail = '0',
  Success = '1',
  NoPermissions = '2'
}

export interface IResult<T = unknown> {
  code: ResultCode;
  message: string;
  data?: T;
  errMsg?: string;
}

/**
 * 通用返回结果类
 */
export class CommonResult<T> implements IResult<T> {
  public code: ResultCode;
  public message: string;
  public data: T;
  public errMsg?: string;

  constructor(code: ResultCode, message: string, data?: T, errMsg?: string) {
    this.code = code;
    this.message = message;
    this.data = data;
    this.errMsg = errMsg;
  }

  public static successWithData<R>(data: R, message: string = 'success') {
    return new CommonResult(ResultCode.Success, message, data);
  }

  public static successByMessage(message: string = 'success') {
    return new CommonResult(ResultCode.Success, message, null);
  }

  public static fail(message: string = 'fail') {
    return new CommonResult(ResultCode.Fail, message, null);
  }

  public static error(message: string = 'error', errMsg = '') {
    return new CommonResult(ResultCode.Fail, message, null, errMsg);
  }
}
